
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.controller' );
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');
require_once(JPATH_COMPONENT.DS.'models'.DS.'helpers'.DS.'RestUtils.php');
require_once(JPATH_COMPONENT.DS.'models'.DS.'helpers'.DS.'RestRequest.php');
class ActinMapControllerRest extends JController{
	
	public function events(){
		$modeloEvento= $this->getModel('Event','ModelActinMap');
		$factoria = DTOFactory::getInstance();
		$restRequest = RestUtils::processRequest();
		switch($restRequest->getMethod())
		{
			case 'get':
				// delegamos al modelo para que devuelva la lista de eventos
				// nos devuelve una ristra de DTOEvent
				$idUser=JFactory::getUser()->id;;
				if($idUser)
				{
					$listaDTOEvent=$modeloEvento->getUserEvents($idUser);
				}
				else{
					
					$listaDTOEvent=$modeloEvento->getEvents();
				}
				RestUtils::sendResponse(200, json_encode($listaDTOEvent), 'application/json');  
				break;
			case 'post':
				// delegamos al modelo para que actualice o cree un evento
				// nos devuelve la id para que la mandemos devuelta
				// el objeto JSON que nos envia el cliente ha de tener minimo nombre, date, coord.
				$jsonEvent=$restRequest->getData();
				$dtoEvent=$modeloEvento->saveEvent($factoria->JSONEvent2DTOEvent($jsonEvent));
				//el servidor envia el json de respuesta guardado en la bd
				RestUtils::sendResponse(200, json_encode($dtoEvent));
				break;
		}
	}
	
	public function updateEvent(){
		
		$modeloEvento= $this->getModel('Event','ModelActinMap');
		$factoria = DTOFactory::getInstance();
		$restRequest = RestUtils::processRequest();		
		switch($restRequest->getMethod())
		{				
			case 'post':
							
				$userCreador=new stdClass();
				$idUser=JFactory::getUser()->id;
				//$AreaAdminDto = $this->createDTOAreaAdministrativa();	
				$jsonEvent=$restRequest->getData();
				$dtoEventDB=$modeloEvento->isEvent($jsonEvent->_idEvent);
			
	    		$userCreador->_userCreator=$modeloEvento->isUserCreator($jsonEvent->_idEvent,$idUser);
	    		
				if ($userCreador->_userCreator) //Si el usuario es creador (true)
				{
					$dtoEvent=$modeloEvento->updateEvent($factoria->JSONUpdateEvent2DTOEvent($dtoEventDB,$jsonEvent,$idUser));
					RestUtils::sendResponse(200, json_encode($dtoEvent));
					return true;
				}
				else
				{
					return $userCreador->_userCreator=false;
				}
				break;
		}
	}
	public function eventSuscription(){
		$modeloEvento= $this->getModel('Event','ModelActinMap');
		$factoria = DTOFactory::getInstance();
		$restRequest = RestUtils::processRequest();
		switch($restRequest->getMethod())
		{
			case 'get':  
				$requestObjet=$restRequest->getRequestVars();
				$idEvent=$requestObjet['idEvent'];
				
				//$idUser=$requestObjet['idUsuario'];
				$idUser=JFactory::getUser()->id;
			
				if($idUser){
					$suscribed=new stdClass();
					$userCreador->_userCreator=$modeloEvento->isUserCreator($idEvent,$idUser);
					if($userCreador->_userCreator==false)
					{
						$suscribed->_suscribedToEvent=$modeloEvento->isUserSubscribed($idEvent,$idUser);
						if($suscribed->_suscribedToEvent)
						 $suscribed->_suscribedToEvent=1;//si no soy creador y estoy suscrito
						else  $suscribed->_suscribedToEvent=2;//si no soy creador y NO estoy suscrito
					}
					else
						$suscribed->_suscribedToEvent=3;//soy  el creador
				}
				else{
					$suscribed->_suscribedToEvent=4;//sin logearse
				}
				RestUtils::sendResponse(200, json_encode($suscribed), 'application/json');
				break;
			case 'post':
				$idUser=JFactory::getUser()->id;
				$jsonEvent=$restRequest->getData();
				$dtoEventSuscription=$modeloEvento->suscribeEvent($factoria->JSONEventSuscription2DTOEvent($jsonEvent,$idUser));
				RestUtils::sendResponse(200,json_encode($dtoEventSuscription));
				break;
		}
	}
	
	public function eventView(){
		$modeloEvento= &$this->getModel('event','ModelActinMap');
		$factoria = DTOFactory::getInstance();
		$restRequest = RestUtils::processRequest();
		switch($restRequest->getMethod())
		{
			case 'get':  
				$requestObject=$restRequest->getRequestVars();
				$id=$requestObject['idEvent'];
				$visitas=new stdClass();
				$visitas->_contadorVisitas=$modeloEvento->getEventViews($id);
				RestUtils::sendResponse(200, json_encode($visitas), 'application/json');
				break;
			case 'post':
				$jsonObject=$restRequest->getData();
				$ok=$modeloEvento->addEventView($jsonObject->_idEvent);
				RestUtils::sendResponse(200,$ok);
				break;
		}
	}
	public function eventSuscriptionDelete(){
		$modeloEvento= $this->getModel('Event','ModelActinMap');
		$factoria = DTOFactory::getInstance();
		$restRequest = RestUtils::processRequest();
		switch($restRequest->getMethod())
		{
			case 'post':
				$jsonObject=$restRequest->getData();
				$idEvent=$jsonObject->idEvento;
				
				$idUser=JFactory::getUser()->id;
			
				if($modeloEvento->isUserSubscribed($idEvent,$idUser)){
					//borramos
					$modeloEvento->deleteEventSubscription($idEvent,$idUser);
					RestUtils::sendResponse(200,"correctly deleted");
				}else{
					//no encontrado
					RestUtils::sendResponse(404);
				}
				break;
		}
	}
	
	public function deleteEvent(){
		$modeloEvento=$this->getModel('Event','ModelActinMap');
		$factoria = DTOFactory::getInstance();
		$restRequest = RestUtils::processRequest();
		switch ($restRequest->getMethod())
		{
			case 'post':
				$jsonObject=$restRequest->getData();
				$idEvent=$jsonObject->idEvento;
				if($modeloEvento->isEvent($idEvent)){
					$modeloEvento->deleteEvent($idEvent);
					RestUtils::sendResponse(200,"correctly deleted");
				}
				else{
					RestUtils::sendResponse(404,"No se ha encontrado el evento");
				}					
			break;
		}
	}
	public function comments(){
		$modeloEvento=$this->getModel('Event','ModelActinMap');
		$factoria = DTOFactory::getInstance();
		$restRequest = RestUtils::processRequest();
		switch ($restRequest->getMethod())
		{
			case 'get':  
				$requestObject=$restRequest->getRequestVars();
				$id=$requestObject['idEvent'];
				if($modeloEvento->isEvent($id)){
					$comments=$modeloEvento->getEventComments($id);
					if(count($comments)){
						RestUtils::sendResponse(200, json_encode($comments), 'application/json');
					}
					else{
						RestUtils::sendResponse(200);
					}
				}else{
					//no encontrado
					RestUtils::sendResponse(404,"Objeto no encontrado");
				}
				
				break;
			case 'post':
				$jsonEventComment=$restRequest->getData();
				if($modeloEvento->isEvent($jsonEventComment->_idEvento)){
					$modeloEvento->setEventComments($factoria->JSONEventComment2DTOEventComment($jsonEventComment));
				}
				else{
					RestUtils::sendResponse(404,"Objeto no encontrado");
				}					
			break;
		}
	}
}
